package com.jingtai.platform.service.shop.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jingtai.common.enums.DeleteFlagEnum;
import com.jingtai.common.enums.EnableStatusEnum;
import com.jingtai.common.template.ResultMapInfo;
import com.jingtai.core.entity.shop.GoodsCategory;
import com.jingtai.core.mapper.shop.GoodsCategoryMapper;
import com.jingtai.platform.service.shop.GoodsCategoryService;
import com.jingtai.platform.util.ResultUtil;
import com.jingtai.platform.util.UrlToMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 商品分类表 impl
 *
 * @author sunlaiqian
 * @date 2020-12-02 18:31:33
 */
@Service
public class GoodsCategoryServiceImpl extends ServiceImpl<GoodsCategoryMapper, GoodsCategory> implements GoodsCategoryService {

    @Autowired
    private GoodsCategoryService goodsCategoryService;

    @Override
    public ResultUtil GoodsCategorylist(Integer page, Integer limit, String conditions){
        QueryWrapper<GoodsCategory> wrapper = new QueryWrapper<>();
        Map<String, String> map = UrlToMap.paramToMap(conditions);
        for(Map.Entry<String, String> entry : map.entrySet()){
            wrapper.like(entry.getKey(), entry.getValue());
        }
        wrapper.eq("delete_flag", DeleteFlagEnum.NOT_DELETE.getCode());
        return new ResultUtil(ResultMapInfo.GETSUCCESS,goodsCategoryService.page(new Page<>(page,limit),wrapper));
    }

    @Override
    public ResultUtil addGoodsCategory(GoodsCategory goodsCategory) {
        boolean save = goodsCategoryService.save(goodsCategory);
        return new ResultUtil(save ? ResultMapInfo.ADDSUCCESS:ResultMapInfo.ADDFAIL);
    }

    @Override
    public ResultUtil getGoodsCategoryById(Long id) {
        GoodsCategory goodsCategory = goodsCategoryService.getById(id);
        return goodsCategory == null ?  new ResultUtil(ResultMapInfo.GETFAIL) : new ResultUtil(ResultMapInfo.GETSUCCESS,goodsCategory);
    }

    @Override
    public ResultUtil delGoodsCategoryById(Long id) {
        GoodsCategory goodsCategory = new GoodsCategory();
        goodsCategory.setId(id);
        goodsCategory.setDeleteFlag(DeleteFlagEnum.DELETE.getCode());
        boolean delState = goodsCategoryService.updateById(goodsCategory);
        return delState ?  new ResultUtil(ResultMapInfo.DELETESUCCESS) : new ResultUtil(ResultMapInfo.DELETEFAIL);
    }

    @Override
    public ResultUtil selectAllCategory() {
        //先查询一级分类
        List<GoodsCategory> list = goodsCategoryService.list(Wrappers.lambdaQuery(GoodsCategory.class).eq(GoodsCategory::getLevel, 1).eq(GoodsCategory::getEnableState, EnableStatusEnum.NORMAL.getValue()).eq(GoodsCategory::getDeleteFlag, DeleteFlagEnum.NOT_DELETE.getCode()).orderByDesc(GoodsCategory::getWeight));
        if (list.size() < 0){
            return new ResultUtil(ResultMapInfo.GETSUCCESS);
        }
        //根据一级分类查询二级分类
        List<JSONObject> jsonObjectList = new ArrayList<>();
        for(GoodsCategory goodsCategory : list){
            List<GoodsCategory> list2 = goodsCategoryService.list(Wrappers.lambdaQuery(GoodsCategory.class).eq(GoodsCategory::getLevel, 2).eq(GoodsCategory::getOneLevelId,goodsCategory.getId()).eq(GoodsCategory::getEnableState, EnableStatusEnum.NORMAL.getValue()).eq(GoodsCategory::getDeleteFlag, DeleteFlagEnum.NOT_DELETE.getCode()).orderByDesc(GoodsCategory::getWeight));
            //根据二级分类查询三级分类
            List<JSONObject> jsonObjectList2 = new ArrayList<>();
            for(GoodsCategory goodsCategory2 : list2){
                List<GoodsCategory> list3 = goodsCategoryService.list(Wrappers.lambdaQuery(GoodsCategory.class).eq(GoodsCategory::getLevel, 3).eq(GoodsCategory::getOneLevelId,goodsCategory.getId()).eq(GoodsCategory::getTwoLevelId,goodsCategory2.getId()).eq(GoodsCategory::getEnableState, EnableStatusEnum.NORMAL.getValue()).eq(GoodsCategory::getDeleteFlag, DeleteFlagEnum.NOT_DELETE.getCode()).orderByDesc(GoodsCategory::getWeight));
                JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(goodsCategory2));
                jsonObject.put("list",list3);
                jsonObjectList2.add(jsonObject);
            }
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(goodsCategory));
            jsonObject.put("list",jsonObjectList2);
            jsonObjectList.add(jsonObject);
        }
        return new ResultUtil(ResultMapInfo.GETSUCCESS,jsonObjectList);
    }

    @Override
    public ResultUtil selectGoodsCategoryOfOne() {
        List<GoodsCategory> list = goodsCategoryService.list(Wrappers.lambdaQuery(GoodsCategory.class).eq(GoodsCategory::getEnableState, EnableStatusEnum.NORMAL.getValue()).eq(GoodsCategory::getDeleteFlag, DeleteFlagEnum.NOT_DELETE.getCode()).eq(GoodsCategory::getLevel, 1));
        return new ResultUtil(ResultMapInfo.GETSUCCESS,list);
    }

    @Override
    public ResultUtil selectGoodsCategoryOfTwoById(Long id) {
        List<GoodsCategory> list = goodsCategoryService.list(Wrappers.lambdaQuery(GoodsCategory.class).eq(GoodsCategory::getEnableState, EnableStatusEnum.NORMAL.getValue()).eq(GoodsCategory::getDeleteFlag, DeleteFlagEnum.NOT_DELETE.getCode()).eq(GoodsCategory::getLevel, 2).eq(GoodsCategory::getOneLevelId,id));
        return new ResultUtil(ResultMapInfo.GETSUCCESS,list);
    }

    @Override
    public ResultUtil editGoodsCategory(GoodsCategory goodsCategory) {
        boolean editState = goodsCategoryService.updateById(goodsCategory);
        return editState ?  new ResultUtil(ResultMapInfo.EDITSUCCESS) : new ResultUtil(ResultMapInfo.EDITFAIL);
    }

}
